home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectSound / AmplitudeModulation / amplitudemodulation.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  17.1 KB  |  517 lines

  1. //----------------------------------------------------------------------------
  2. // File: AmplitudeModulation.cpp
  3. //
  4. // Desc: AmplitudeModulation sample shows how to create an effect buffer and 
  5. //       adjust amplitude modulation parameters.
  6. //
  7. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define STRICT
  10. #include <windows.h>
  11. #include <basetsd.h>
  12. #include <mmsystem.h>
  13. #include <mmreg.h>
  14. #include <dxerr8.h>
  15. #include <dsound.h>
  16. #include <cguid.h>
  17. #include <commctrl.h>
  18. #include <commdlg.h>
  19. #include <dsound.h>
  20. #include "resource.h"
  21. #include "DSUtil.h"
  22. #include "DXUtil.h"
  23.  
  24.  
  25.  
  26. //-----------------------------------------------------------------------------
  27. // Function-prototypes
  28. //-----------------------------------------------------------------------------
  29. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg,  WPARAM wParam, LPARAM lParam );
  30. VOID    OnInitDialog( HWND hDlg );
  31. VOID    OnTimer( HWND hDlg );
  32. VOID    OnOpenSoundFile( HWND hDlg );
  33. HRESULT ValidateWaveFile( HWND hDlg, TCHAR* strFileName );
  34. HRESULT OnPlaySound( HWND hDlg );
  35. HRESULT CreateAndFillBuffer( HWND hDlg, DWORD dwCreationFlags );
  36. VOID    OnEffectChanged( HWND hDlg );
  37. VOID    SetBufferOptions( LONG lFrequency, LONG lPan, LONG lVolume );
  38. VOID    EnablePlayUI( HWND hDlg, BOOL bEnable );
  39.  
  40.  
  41.  
  42.  
  43. //-----------------------------------------------------------------------------
  44. // Defines, constants, and global variables
  45. //-----------------------------------------------------------------------------
  46. #define DSFX_GARGLE_RATEHZ_MAX 1000
  47. #define DSFX_GARGLE_RATEHZ_MIN 1
  48.  
  49. TCHAR                       g_strWaveFileName[MAX_PATH];
  50. CSoundManager*              g_pSoundManager     = NULL;
  51. CSound*                     g_pSound            = NULL;
  52. LPDIRECTSOUNDFXGARGLE       g_pIGargle          = NULL;
  53. HINSTANCE                   g_hInst             = NULL;
  54.  
  55.  
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // Name: WinMain()
  60. // Desc: Entry point for the application.  Since we use a simple dialog for 
  61. //       user interaction we don't need to pump messages.
  62. //-----------------------------------------------------------------------------
  63. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, INT nCmdShow )
  64. {
  65.     g_hInst = hInst;
  66.  
  67.     CoInitialize( NULL );
  68.  
  69.     // Init the common control dll 
  70.     InitCommonControls();
  71.  
  72.     // Display the main dialog box.
  73.     DialogBox( hInst, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc );
  74.  
  75.     CoUninitialize();
  76.  
  77.     return TRUE;
  78. }
  79.  
  80.  
  81.  
  82.  
  83. //-----------------------------------------------------------------------------
  84. // Name: MainDlgProc()
  85. // Desc: Handles dialog messages
  86. //-----------------------------------------------------------------------------
  87. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  88. {
  89.     HRESULT hr;
  90.  
  91.     switch( msg ) 
  92.     {
  93.         case WM_COMMAND:
  94.             switch( LOWORD(wParam) )
  95.             {
  96.                 case IDCANCEL:
  97.                     EndDialog( hDlg, IDCANCEL );
  98.                     break;
  99.  
  100.                 case IDC_SOUNDFILE:
  101.                     OnOpenSoundFile( hDlg );
  102.                     break;
  103.  
  104.                 case IDC_PLAY:
  105.                     if( FAILED( hr = OnPlaySound( hDlg ) ) )
  106.                     {
  107.                         DXTRACE_ERR( TEXT("OnPlaySound"), hr );
  108.                         MessageBox( hDlg, "Error playing DirectSound buffer."
  109.                                     "Sample will now exit.", "DirectSound Sample", 
  110.                                     MB_OK | MB_ICONERROR );
  111.                         EndDialog( hDlg, IDABORT );
  112.                     }
  113.  
  114.                     break;
  115.  
  116.                 case IDC_STOP:
  117.                     SAFE_RELEASE( g_pIGargle );
  118.                     if( g_pSound )
  119.                     {
  120.                         g_pSound->Stop();
  121.                         g_pSound->Reset();
  122.                     }
  123.                     break;
  124.  
  125.                 case IDC_WAVEFORM_TRIANGLE:
  126.                 case IDC_WAVEFORM_SQUARE:
  127.                     OnEffectChanged( hDlg );
  128.                     break;
  129.  
  130.                 default:
  131.                     return FALSE; // Didn't handle message
  132.             }
  133.             break;
  134.  
  135.         case WM_TIMER:
  136.             OnTimer( hDlg );
  137.             break;
  138.  
  139.         case WM_INITDIALOG:
  140.             OnInitDialog( hDlg );
  141.             break;
  142.  
  143.         case WM_NOTIFY:
  144.             OnEffectChanged( hDlg );
  145.             break;
  146.  
  147.         case WM_DESTROY:
  148.             // Cleanup everything
  149.             KillTimer( hDlg, 1 );
  150.             SAFE_RELEASE( g_pIGargle );
  151.             SAFE_DELETE( g_pSound );
  152.             SAFE_DELETE( g_pSoundManager );
  153.             break; 
  154.  
  155.         default:
  156.             return FALSE; // Didn't handle message
  157.     }
  158.  
  159.     return TRUE; // Handled message
  160. }
  161.  
  162.  
  163.  
  164.  
  165. //-----------------------------------------------------------------------------
  166. // Name: OnInitDialog()
  167. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  168. //-----------------------------------------------------------------------------
  169. VOID OnInitDialog( HWND hDlg )
  170. {
  171.     HRESULT hr;
  172.  
  173.     // Load the icon
  174.     HICON hIcon = LoadIcon( g_hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  175.  
  176.     // Create a static IDirectSound in the CSound class.  
  177.     // Set coop level to DSSCL_PRIORITY, and set primary buffer 
  178.     // format to stereo, 22kHz and 16-bit output.
  179.     g_pSoundManager = new CSoundManager();
  180.  
  181.     if( FAILED( hr = g_pSoundManager->Initialize( hDlg, DSSCL_PRIORITY, 2, 22050, 16 ) ) )
  182.     {
  183.         DXTRACE_ERR( TEXT("Initialize"), hr );
  184.         MessageBox( hDlg, "Error initializing DirectSound.  Sample will now exit.", 
  185.                             "DirectSound Sample", MB_OK | MB_ICONERROR );
  186.         EndDialog( hDlg, IDABORT );
  187.         return;
  188.     }
  189.  
  190.     // Set the icon for this dialog.
  191.     PostMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  192.     PostMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  193.  
  194.     // Create a timer, so we can check for when the soundbuffer is stopped
  195.     SetTimer( hDlg, 0, 250, NULL );
  196.  
  197.     // Get handles to dialog items
  198.     HWND hFreqSlider    = GetDlgItem( hDlg, IDC_FREQUENCY_SLIDER );
  199.  
  200.     // Set UI defaults 
  201.     CheckDlgButton( hDlg, IDC_LOOP_CHECK, BST_CHECKED );
  202.     CheckRadioButton( hDlg, IDC_WAVEFORM_TRIANGLE, IDC_WAVEFORM_TRIANGLE, IDC_WAVEFORM_TRIANGLE );
  203.  
  204.     // Set the range and position of the freq slider from 
  205.     // DSBFREQUENCY_MIN and DSBFREQUENCY_MAX are DirectSound constants
  206.     PostMessage( hFreqSlider, TBM_SETRANGEMAX, TRUE, DSFX_GARGLE_RATEHZ_MAX );
  207.     PostMessage( hFreqSlider, TBM_SETRANGEMIN, TRUE, DSFX_GARGLE_RATEHZ_MIN );
  208.     PostMessage( hFreqSlider, TBM_SETPOS, TRUE, DSFX_GARGLE_RATEHZ_MIN );
  209.  
  210.     // Load default wave file
  211.     TCHAR strFile[MAX_PATH];
  212.     GetWindowsDirectory( strFile, MAX_PATH );
  213.     lstrcat( strFile, "\\media\\ding.wav" );
  214.     
  215.     if( FAILED( hr = ValidateWaveFile( hDlg, strFile ) ) )
  216.     {
  217.         // Set the UI controls
  218.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  219.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("No file loaded.") );
  220.     }
  221. }
  222.  
  223.  
  224.  
  225.  
  226. //-----------------------------------------------------------------------------
  227. // Name: OnOpenSoundFile()
  228. // Desc: Called when the user requests to open a sound file
  229. //-----------------------------------------------------------------------------
  230. VOID OnOpenSoundFile( HWND hDlg ) 
  231. {
  232.     static TCHAR strFileName[MAX_PATH] = TEXT("");
  233.     static TCHAR strPath[MAX_PATH] = TEXT("");
  234.  
  235.     // Setup the OPENFILENAME structure
  236.     OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
  237.                          TEXT("Wave Files\0*.wav\0All Files\0*.*\0\0"), NULL,
  238.                          0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
  239.                          TEXT("Open Sound File"),
  240.                          OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
  241.                          TEXT(".wav"), 0, NULL, NULL };
  242.  
  243.     // Get the default media path (something like C:\WINDOWS\MEDIA)
  244.     if( '\0' == strPath[0] )
  245.     {
  246.         GetWindowsDirectory( strPath, MAX_PATH );
  247.         if( strcmp( &strPath[strlen(strPath)], TEXT("\\") ) )
  248.             strcat( strPath, TEXT("\\") );
  249.         strcat( strPath, TEXT("MEDIA") );
  250.     }
  251.  
  252.     // Update the UI controls to show the sound as loading a file
  253.     EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), FALSE );
  254.     EnableWindow( GetDlgItem( hDlg, IDC_STOP ), FALSE );
  255.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Loading file...") );
  256.  
  257.     SAFE_RELEASE( g_pIGargle );
  258.     if( g_pSound )
  259.     {
  260.         g_pSound->Stop();
  261.         g_pSound->Reset();
  262.     }
  263.  
  264.     // Display the OpenFileName dialog. Then, try to load the specified file
  265.     if( TRUE != GetOpenFileName( &ofn ) )
  266.     {
  267.         if( g_pSound )
  268.         {
  269.             EnableWindow( GetDlgItem( hDlg, IDC_PLAY ), TRUE );
  270.             EnableWindow( GetDlgItem( hDlg, IDC_STOP ), TRUE );
  271.         }
  272.  
  273.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Load aborted.") );
  274.         return;
  275.     }
  276.  
  277.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  278.  
  279.     // Make sure wave file is a valid wav file
  280.     ValidateWaveFile( hDlg, strFileName );
  281.  
  282.     // Remember the path for next time
  283.     strcpy( strPath, strFileName );
  284.     char* strLastSlash = strrchr( strPath, '\\' );
  285.     strLastSlash[0] = '\0';
  286. }
  287.  
  288.  
  289.  
  290.  
  291. //-----------------------------------------------------------------------------
  292. // Name: ValidateWaveFile()
  293. // Desc: Open the wave file with the helper 
  294. //       class CWaveFile to make sure it is valid
  295. //-----------------------------------------------------------------------------
  296. HRESULT ValidateWaveFile( HWND hDlg, TCHAR* strFileName )
  297. {
  298.     HRESULT hr;
  299.     CWaveFile waveFile;
  300.  
  301.     if( -1 == GetFileAttributes(strFileName) )
  302.         return E_FAIL;
  303.     
  304.     // Load the wave file
  305.     if( FAILED( hr = waveFile.Open( strFileName, NULL, WAVEFILE_READ ) ) )
  306.     {        
  307.         waveFile.Close();
  308.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Bad wave file.") );
  309.         return DXTRACE_ERR( TEXT("Open"), hr );
  310.     }
  311.     else // The load call succeeded
  312.     {
  313.         WAVEFORMATEX* pwfx = waveFile.GetFormat();
  314.         if( pwfx->wFormatTag != WAVE_FORMAT_PCM )
  315.         {
  316.             // Sound must be PCM when using DSBCAPS_CTRLFX            
  317.             SAFE_RELEASE( g_pIGargle );
  318.             SAFE_DELETE( g_pSound );
  319.             SetDlgItemText( hDlg, IDC_STATUS, TEXT("Wave file must be PCM for effects control.") );
  320.             SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  321.             return S_FALSE;
  322.         }
  323.   
  324.         // Update the UI controls to show the sound as the file is loaded
  325.         waveFile.Close();
  326.  
  327.         EnablePlayUI( hDlg, TRUE );
  328.         SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
  329.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("File loaded.") );
  330.         strcpy( g_strWaveFileName, strFileName );
  331.  
  332.         // Get the samples per sec from the wave file
  333.         DWORD dwSamplesPerSec = waveFile.m_pwfx->nSamplesPerSec;
  334.         return S_OK;
  335.     }
  336. }
  337.  
  338.  
  339.  
  340.  
  341. //-----------------------------------------------------------------------------
  342. // Name: OnPlaySound()
  343. // Desc: User hit the "Play" button
  344. //-----------------------------------------------------------------------------
  345. HRESULT OnPlaySound( HWND hDlg ) 
  346. {
  347.     HRESULT                 hr;
  348.     DWORD                   dwCreationFlags;
  349.     DWORD                   dwResults;
  350.  
  351.     LPDIRECTSOUNDBUFFER  pDSB = NULL;
  352.     LPDIRECTSOUNDBUFFER8 pDSB8 = NULL;
  353.  
  354.     BOOL bLooped        = ( IsDlgButtonChecked( hDlg, IDC_LOOP_CHECK )     == BST_CHECKED );
  355.  
  356.     // We would only use CTRLFX control on dsound buffer
  357.     dwCreationFlags = DSBCAPS_CTRLFX;
  358.  
  359.     // Free any previous sound and FXs
  360.     SAFE_RELEASE( g_pIGargle );
  361.     SAFE_DELETE( g_pSound );
  362.  
  363.     // Since the user can change the focus before the sound is played, 
  364.     // we need to create the sound buffer every time the play button is pressed 
  365.  
  366.     // Load the wave file into a DirectSound buffer
  367.     if( FAILED( hr = g_pSoundManager->Create( &g_pSound, g_strWaveFileName, dwCreationFlags, GUID_NULL ) ) )
  368.     {
  369.         // Not a critical failure, so just update the status
  370.         DXTRACE_ERR_NOMSGBOX( TEXT("Create"), hr );
  371.         if( hr == DSERR_BUFFERTOOSMALL )
  372.         {
  373.             // DSERR_BUFFERTOOSMALL will be returned if the buffer is
  374.             // less than DSBSIZE_FX_MIN (100ms) and the buffer is created
  375.             // with DSBCAPS_CTRLFX.                           
  376.             SetDlgItemText( hDlg, IDC_STATUS, TEXT("Wave file is too short (less than 100ms) for effect processing.") );
  377.         }
  378.         else
  379.         {
  380.             SetDlgItemText( hDlg, IDC_STATUS, TEXT("Could not create sound buffer.") );
  381.         }
  382.         
  383.         return S_FALSE; 
  384.     }
  385.  
  386.     // Query IDirectSoundBuffer8 interface
  387.     pDSB = g_pSound->GetBuffer( 0 );
  388.     if( FAILED( hr = pDSB->QueryInterface( IID_IDirectSoundBuffer8, (LPVOID*) &pDSB8 ) ) )
  389.         return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  390.  
  391.     // Set gargle effect on the IDirectSoundBuffer8
  392.     DSEFFECTDESC dsed;
  393.     ZeroMemory( &dsed, sizeof(DSEFFECTDESC) );
  394.     dsed.dwSize       = sizeof(DSEFFECTDESC);
  395.     dsed.dwFlags      = 0;
  396.     dsed.guidDSFXClass = GUID_DSFX_STANDARD_GARGLE;
  397.  
  398.     if( FAILED( hr = pDSB8->SetFX( 1, &dsed, &dwResults ) ) )
  399.     {
  400.         // Not a critical failure, so just update the status
  401.         DXTRACE_ERR( TEXT("SetFX"), hr );
  402.         SetDlgItemText( hDlg, IDC_STATUS, TEXT("Could not set gargle effect.") );
  403.         return S_FALSE;
  404.     }
  405.  
  406.     // Get gargle effect friendly interface
  407.     if( FAILED( hr = pDSB8->GetObjectInPath( GUID_DSFX_STANDARD_GARGLE, 0, 
  408.                                              IID_IDirectSoundFXGargle, 
  409.                                              (LPVOID*) &g_pIGargle ) ) )
  410.         return DXTRACE_ERR( TEXT("GetObjectInPath"), hr );
  411.  
  412.     // Cleanup
  413.     SAFE_RELEASE( pDSB8 );
  414.  
  415.     // Set the buffer options to what the sliders are set to
  416.     OnEffectChanged( hDlg );
  417.  
  418.     // Play the sound
  419.     DWORD dwLooped = bLooped ? DSBPLAY_LOOPING : 0L;
  420.     if( FAILED( hr = g_pSound->Play( 0, dwLooped ) ) )
  421.         return DXTRACE_ERR( TEXT("Play"), hr );
  422.  
  423.     // Update the UI controls to show the sound as playing
  424.     EnablePlayUI( hDlg, FALSE );
  425.     SetDlgItemText( hDlg, IDC_STATUS, TEXT("Sound playing.") );
  426.  
  427.     return S_OK;
  428. }
  429.  
  430.  
  431.  
  432.  
  433. //-----------------------------------------------------------------------------
  434. // Name: OnTimer()
  435. // Desc: When we think the sound is playing this periodically checks to see if 
  436. //       the sound has stopped.  If it has then updates the dialog.
  437. //-----------------------------------------------------------------------------
  438. VOID OnTimer( HWND hDlg ) 
  439. {
  440.     if( IsWindowEnabled( GetDlgItem( hDlg, IDC_STOP ) ) )
  441.     {
  442.         // We think the sound is playing, so see if it has stopped yet.
  443.         if( !g_pSound->IsSoundPlaying() ) 
  444.         {
  445.             // Update the UI controls to show the sound as stopped
  446.             EnablePlayUI( hDlg, TRUE );
  447.             SetDlgItemText( hDlg, IDC_STATUS, TEXT("Sound stopped.") );
  448.         }
  449.     }
  450. }
  451.  
  452.  
  453.  
  454.  
  455. //-----------------------------------------------------------------------------
  456. // Name: OnEffectChanged()  
  457. // Desc: Called when the UI prompted an effect change
  458. //-----------------------------------------------------------------------------
  459. VOID OnEffectChanged( HWND hDlg )
  460. {
  461.     DSFXGargle dsfxGargle;
  462.  
  463.     // Get handles to dialog items
  464.     HWND hFreqSlider = GetDlgItem( hDlg, IDC_FREQUENCY_SLIDER );
  465.  
  466.     // Get the position of the sliders
  467.     dsfxGargle.dwRateHz = (DWORD)SendMessage( hFreqSlider, TBM_GETPOS, 0, 0 );
  468.  
  469.     // Update UI
  470.     TCHAR strBuffer[10];
  471.     wsprintf( strBuffer, TEXT("%ld"), dsfxGargle.dwRateHz );
  472.     SetDlgItemText( hDlg, IDC_FREQUENCY, strBuffer );
  473.  
  474.     // Get wave form
  475.     if( IsDlgButtonChecked( hDlg, IDC_WAVEFORM_SQUARE ) == BST_CHECKED )
  476.         dsfxGargle.dwWaveShape = DSFXGARGLE_WAVE_SQUARE;
  477.     else 
  478.         dsfxGargle.dwWaveShape = DSFXGARGLE_WAVE_TRIANGLE;
  479.  
  480.     // Set the options in the DirectSound buffer
  481.     if( g_pSound && g_pIGargle )
  482.     {
  483.         g_pIGargle->SetAllParameters( &dsfxGargle );
  484.     }
  485. }
  486.  
  487.  
  488.  
  489.  
  490. //-----------------------------------------------------------------------------
  491. // Name: EnablePlayUI()
  492. // Desc: Enables or disables the Play UI controls 
  493. //-----------------------------------------------------------------------------
  494. VOID EnablePlayUI( HWND hDlg, BOOL bEnable )
  495. {
  496.     if( bEnable )
  497.     {
  498.         EnableWindow( GetDlgItem( hDlg, IDC_LOOP_CHECK      ), TRUE );
  499.         EnableWindow( GetDlgItem( hDlg, IDC_PLAY            ), TRUE );
  500.         EnableWindow( GetDlgItem( hDlg, IDC_STOP            ), FALSE );
  501.         SetFocus(     GetDlgItem( hDlg, IDC_PLAY ) );
  502.     }
  503.     else
  504.     {
  505.         EnableWindow( GetDlgItem( hDlg, IDC_LOOP_CHECK      ), FALSE );
  506.         EnableWindow( GetDlgItem( hDlg, IDC_PLAY            ), FALSE );
  507.         EnableWindow( GetDlgItem( hDlg, IDC_STOP            ), TRUE );
  508.         SetFocus(     GetDlgItem( hDlg, IDC_STOP ) );
  509.     }
  510. }
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.